package com.sntk.organauthomanager.infrastructure.service;

import com.sntk.organauthomanager.infrastructure.exception.DataNoLegalException;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * @ClassName: ServiceInfo
 * @author: wgchao
 * @createTime: 2020/9/7 4:21 PM
 * @Description: TODO
 */
@Getter
public class ServiceConfigInfo {

    //服务编号
    private String serviceCode;

    //注册服务器IP
    private String registeredIp;

    //服务名称
    private String serviceName;

    //检测地址
    private String detectUrl;

    //失联次数
    private Integer numberOfLostConnections = 0;

    public ServiceConfigInfo(String serviceCode, String serviceName, String registeredIp, String detectUrl) {
        if(StringUtils.isEmpty(serviceCode)){
            throw new DataNoLegalException("服务注册编号缺失");
        }
        if(StringUtils.isEmpty(detectUrl)){
            throw new DataNoLegalException("服务检测地址缺失");
        }
        this.serviceCode = serviceCode;
        this.serviceName = serviceName;
        this.registeredIp = registeredIp;
        this.detectUrl = detectUrl;
    }

    /**
     * 失联
     * @return 连续失联超过三次 返回true 删除该服务
     */
    public boolean lostConnection(){
        this.numberOfLostConnections += 1;
        if(this.numberOfLostConnections < 3){
            return false;
        }
        return true;
    }

    public void reset(){
        this.numberOfLostConnections = 0;
    }
}
